/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld.market;

import java.util.Hashtable;
import org.sunspotworld.sensortasks.routingtasks.AdvertiseTask;
import org.sunspotworld.sensortasks.SensorTask;

/**
 *
 * @author me
 */
public class MarketAgent {
    private Hashtable bids = new Hashtable();
    private Hashtable reservations = new Hashtable();
    private double totalDemand = 0;
    private double totalSupply = 0;
    private static MarketAgent instance = new MarketAgent();
    
    private MarketAgent() {
        
    }
    
    public double getTotalDemand() {
        return totalDemand;
    }
    
    public double getTotalSupply() {
        return totalSupply;
    }
    
    public static MarketAgent getInstance() {
        return instance;
    }
    
    public void addBid(Bid bid) {
        totalDemand += bid.getValue();
        bids.put(bid.getSlot(), bid);
    }
    
    public void addReservation(Reservation reserve) {
        totalSupply += reserve.getValue();
        reservations.put(reserve.getSlot(), bids);
    }
    
    public void invalidateBid(SensorTask slot) {
        Bid bid = (Bid)bids.remove(slot);
        totalDemand -= bid.getValue();
    }
    
    public void invalidateReservation(AdvertiseTask slot) {
        Reservation reserve = (Reservation)reservations.remove(slot);
        totalSupply -= reserve.getValue();
    }
    
    protected void trimExcessDemand() {
        
    }
    
    protected void releaseExcessSupply() {
        
    }
    
    public void updateValue(SensorTask slot, double value) {
        if (bids.containsKey(slot)) {
            ((Bid)bids.get(slot)).updateValue(value);
        }
    }
    
    public void updateDemand(SensorTask slot, double value) {
        if (bids.containsKey(slot)) {
            ((Bid)bids.get(slot)).updateDemand(value);
        }
    }
}
